﻿using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using studentSubsidy.Entity;
using studentSubsidy.Service;
using studentSubsidy.Utils;

namespace studentSubsidy.Controllers
{
    [Route("api/[action]")]
    [ApiController]
    public class UploadController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;
        private UploadService _uploadService = new UploadService();
        private string mainPath = "F://Projects/.NET/studentSubsidy/studentSubsidy/Public/file";

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        public UploadController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 接收文件
        /// </summary>
        /// <returns></returns>
        [HttpPost(Name = "upload")]
        public Result upload([FromQuery] int id, int projectId, int type)
        {
            IFormFileCollection files = Request.Form.Files;
            string fileNameReturn = "";
            foreach (IFormFile file in files)
            {
                string directory = Path.Combine(mainPath);
                string fileExtension = Path.GetExtension(file.FileName);
                string fileName = file.FileName.Split(".")[0] + "_" + id + "_" + DateTime.Now.ToString("yy-MM-dd") +
                                  fileExtension;
                string filePath = Path.Combine(directory, fileName);
                fileNameReturn = fileName;
                // 生成文件路径
                while (System.IO.File.Exists(filePath))
                {
                    fileName = fileName.Split(".")[0] + GenerateID.GenForInt() + fileExtension;
                    filePath = Path.Combine(directory, fileName);
                    fileNameReturn = fileName;
                }

                // 写入文件
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Flush();
                }

                if (type == 0)
                {
                    _uploadService.addUploadFile(fileName, fileExtension, id, projectId);
                }

                if (type == 2)
                {
                    return _uploadService.addSignedFile(fileName, fileExtension, id, projectId);
                }
            }

            return new Result(200, "上传成功", fileNameReturn, null);
        }

        [HttpGet(Name = "download")]
        public IActionResult Download([FromQuery] string file_name)
        {
            string link = "http://localhost:99/Public/file/" + file_name;
            var net = new System.Net.WebClient();
            var data = net.DownloadData(link);
            var content = new System.IO.MemoryStream(data);
            var contentType = "APPLICATION/octet-stream";
            return File(content, contentType, file_name);
        }

        [HttpGet(Name = "delete")]
        public Result delete([FromQuery] string file_name)
        {
            return new Result(_uploadService.UpLoadDelete(file_name), "", null, null);
        }

        [HttpPost(Name = "uploadsigned")]
        public Result UploadSigned([FromQuery] int type, int applyId, int userId)
        {
            IFormFileCollection files = Request.Form.Files;
            string fileNameReturn = "";
            foreach (IFormFile file in files)
            {
                string directory = Path.Combine(mainPath);
                string fileExtension = Path.GetExtension(file.FileName);
                string fileName = file.FileName.Split(".")[0] + "_" + type + "_" + DateTime.Now.ToString("yy-MM-dd") +
                                  fileExtension;
                string filePath = Path.Combine(directory, fileName);
                fileNameReturn = fileName;
                // 生成文件路径
                while (System.IO.File.Exists(filePath))
                {
                    fileName = fileName.Split(".")[0] + GenerateID.GenForInt() + fileExtension;
                    filePath = Path.Combine(directory, fileName);
                    fileNameReturn = fileName;
                }

                // 写入文件
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Flush();
                }

                return _uploadService.addSignedPicFile(fileName, type, userId, applyId);
            }

            return null;
        }
    }
}